# -*- coding: utf-8 -*-

import sys
import os
import requests

if len(sys.argv) <= 1:
    print("Please input ip address !")
    sys.exit(1)

URL = "http://ip.taobao.com"
INTERFACE = "/service/getIpInfo.php?ip="


def ip_info(IP):
    data = requests.get(URL+INTERFACE+IP)
    data_dict = data.json()['data']
    data_code = data.json()['code']

    if data_code == 0:
        return data_dict["country"] + data_dict["region"] + data_dict["city"] + data_dict["isp"]

if os.path.isfile(sys.argv[1]):
    file_path = sys.argv[1]
    fh = open(file_path, 'r')
    for line in fh.readlines():
        city_address = ip_info(line)
        print(line.strip() + ":" + city_address)
else:
    ip_address = sys.argv[1]
    city_address = ip_info(ip_address)
    print(ip_address + ":" + city_address)
